package org.huang.test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @author hdu_huang
 * @since 2024/1/9 14:48
 * 测试虚拟线程 VirtualThread.DEFAULT_SCHEDULER(ForkJoinPool)  以FIFO模式来进行调度，N默认是为Runtime.availableProcessors()
 */
public class Virtual {
    public static void main(String[] args) throws Exception{

        //1. VirtualThread创建  1.静态构建器 2.线程工厂  3.ExecutorService
        // sleep可能发生底层线程切换

        Thread.Builder.OfVirtual virtual = Thread.ofVirtual();

        Runnable runnable = () -> System.out.println(Thread.currentThread());
        Thread start = virtual.name("virtual1").start(() -> System.out.println(Thread.currentThread()));
        Thread virtualThread = Thread.startVirtualThread(runnable);

        ThreadFactory virtualFactory = virtual.factory();

        virtualFactory.newThread(runnable).start();

        System.out.println(virtual);
        System.out.println(start);

        try (ExecutorService service = Executors.newVirtualThreadPerTaskExecutor()) {
            service.submit(runnable);
        }

        start.join();


    }
}
